﻿//创建时间: 2010-10-18
//创建作者: 李 城
//功能说明: form表单 辅助方法
//最后修改: 2010-10-18
//最后修改: 2010-10-18 
using System;
using System.Collections.Generic;
using System.Text;

using Newtonsoft.Json;


namespace TRPOP.Common
{
    /// <summary>
    /// form表单 辅助方法 【FORM引擎】
    /// </summary>
    public class FormAssist
    {
        /// <summary>
        /// 高度为15px的样式
        /// </summary>
        static string m_txtClassStr = " class=\"txtBlur TextBoxWidth\"  " +
                                             " onfocus=\" this.className=&quot;control_focus TextBoxWidth&quot; \" " +
                                             " onblur=\"  this.className=&quot;control_blur TextBoxWidth&quot;  \" ";
        /// <summary>
        /// 没有设高度的样式
        /// </summary>
        static string m_txtClassStr1 = " class=\"control_blur1 TextBoxWidth\"  " +
                                            " onfocus=\" this.className=&quot;control_focus1 TextBoxWidth&quot; \" " +
                                            " onblur=\"  this.className=&quot;control_blur1 TextBoxWidth&quot;  \" ";

        #region 添加form元素 text

        /// <summary>
        /// 添加form元素 text
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <param name="objTemp">请根据TRPOP.Common.HtmlAttributeKey 进行添加Key、value</param>
        /// <returns></returns>
        public static string Addtext(string name, string id, string value, JavaScriptObject objTemp)
        {
            StringBuilder strb = new StringBuilder();
            strb.Append(string.Format(@"<input "));
            strb.Append(string.Format("   type=\"text\"  id=\"{0}\"  name=\"{1}\"  {2}  value=\"{3}\"  ", id, name, m_txtClassStr, value.HtmlEncode().Replace("\"", "&quot;")));
            #region 添加其他属性
            if (objTemp != null && objTemp.Count > 0)
            {
                //readOnly
                if (objTemp.ContainsKey(HtmlAttributeKey.TextReadOnly) && objTemp[HtmlAttributeKey.TextReadOnly].ToString().ToLower() == "true")
                {
                    strb.Append("   readonly=\"readonly\" ");
                }
                //结合js验证文本框
                if (objTemp.ContainsKey(HtmlAttributeKey.TextRequired) && objTemp[HtmlAttributeKey.TextRequired].ToString().ToLower() == "true")
                {
                    strb.Append(string.Format("   required=\"1\"   "));
                    //prompt
                    if (objTemp.ContainsKey(HtmlAttributeKey.TextPrompt))
                    {
                        strb.Append(string.Format("   prompt=\"{0}\" ", objTemp[HtmlAttributeKey.TextPrompt]));
                    }
                    //valuelength
                    if (objTemp.ContainsKey(HtmlAttributeKey.TextValuelength))
                    {
                        strb.Append(string.Format("   valuelength=\"{0}\" ", objTemp[HtmlAttributeKey.TextValuelength]));
                    }
                    //valuetype
                    if (objTemp.ContainsKey(HtmlAttributeKey.TextValuetype))
                    {
                        strb.Append(string.Format("   valuetype=\"{0}\" ", objTemp[HtmlAttributeKey.TextValuetype]));
                    }
                }
            }
            #endregion
            strb.Append(string.Format(@"/>    "));
            return strb.ToString();
        }

        #endregion

        #region 添加form元素

        /// <summary>
        /// 添加form元素 Input
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <param name="objTemp">添加Key、value属性</param>
        /// <returns></returns>
        public static string AddFormElement(string name, string id, string value, JavaScriptObject objTemp)
        {
            StringBuilder strb = new StringBuilder();
            #region 初始化编辑器宽和高
            string strEditorWidth = "700px", strEditorHeight = "400px";
            if (objTemp.ContainsKey("width"))
            {
                strEditorWidth = objTemp["width"].ToString();
                if (strEditorWidth.IsNumeric())
                {
                    strEditorWidth += "px";
                }
            }
            if (objTemp.ContainsKey("height"))
            {
                strEditorHeight = objTemp["height"].ToString();
                if (strEditorHeight.IsNumeric())
                {
                    strEditorHeight += "px";
                }
            }
            #endregion
            switch (objTemp["type"].ToString())
            {
                case "text":
                    #region 添加input
                    //添加input
                    strb.Append(string.Format(@"<input "));
                    strb.Append(string.Format("   id=\"{0}\"  name=\"{1}\"  {2}  value=\"{3}\"  ", id, name, m_txtClassStr, value.HtmlEncode().Replace("\"", "&quot;")));
                    foreach (string key in objTemp.Keys)
                    {
                        if (key != "inputpicker")
                        {
                            strb.AppendFormat(" {0}=\"{1}\" ", key, objTemp[key]);
                        }
                    }
                    strb.Append(string.Format(@"/>    "));
                    //添加采集功能图标
                    if (objTemp.ContainsKey("inputpicker"))
                    {
                        //颜色
                        if (objTemp["inputpicker"].ToString() == FieldInputPickerType.ColorPicker)
                        {
                            strb.Append("<img src=\"../../App_Themes/Classic/images/menu_open.gif\"  class=\"Dtoolbutton\" onmouseover=\"this.className='Dtoolbutton';\" onmouseout=\"this.className='Dtoolbutton';\" onclick=\"this.className='Ctoolbutton';InputPicker(event,'color', 'document.form1', '" + id + "')\" title=\"\" vspace=\"0\" hspace=\"0\" />");
                        }
                        //时间
                        else if (objTemp["inputpicker"].ToString() == FieldInputPickerType.DatePicker)
                        {
                            strb.Append("<img src=\"../../App_Themes/Classic/images/menu_open.gif\"  class=\"Dtoolbutton\" onmouseover=\"this.className='Dtoolbutton';\" onmouseout=\"this.className='Dtoolbutton';\" onclick=\"this.className='Ctoolbutton';InputPicker(event,'date', 'document.form1', '" + id + "')\" title=\"\" vspace=\"0\" hspace=\"0\" />");
                        }
                        //图片录入
                        else if (objTemp["inputpicker"].ToString() == FieldInputPickerType.ImgPicker)
                        {
                            strb.Append("<img src=\"../../App_Themes/Classic/images/menu_open.gif\"  class=\"Dtoolbutton\" onmouseover=\"this.className='Dtoolbutton';\" onmouseout=\"this.className='Dtoolbutton';\" onclick=\"this.className='Ctoolbutton';commonInputPicker(event,'img', 'document.form1', '" + id + "')\" title=\"\" vspace=\"0\" hspace=\"0\" />");
                        }
                        //附件录入
                        else if (objTemp["inputpicker"].ToString() == FieldInputPickerType.AttachPicker)
                        {
                            strb.Append("<img src=\"../../App_Themes/Classic/images/menu_open.gif\"  class=\"Dtoolbutton\" onmouseover=\"this.className='Dtoolbutton';\" onmouseout=\"this.className='Dtoolbutton';\" onclick=\"this.className='Ctoolbutton';commonInputPicker(event,'attach', 'document.form1', '" + id + "')\" title=\"\" vspace=\"0\" hspace=\"0\" />");
                        }
                    }
                    #endregion
                    break;
                case "textarea":
                    #region 添加 textarea
                    strb.Append(string.Format(@"<textarea "));
                    strb.Append(string.Format("   id=\"{0}\"  name=\"{1}\" {2} ", id, name, m_txtClassStr1));
                    foreach (string key in objTemp.Keys)
                    {
                        strb.AppendFormat(" {0}=\"{1}\" ", key, objTemp[key]);
                    }
                    strb.Append(string.Format(@">{0}</textarea>", value));
                    #endregion
                    break;
                case "radio":
                    #region 添加 radio
                    if (objTemp.ContainsKey("item"))
                    {
                        JavaScriptObject jsItems = (JavaScriptObject)objTemp["item"];
                        strb.Append("<span id=\"" + id + "\">");
                        foreach (string key in jsItems.Keys)
                        {
                            JavaScriptObject jsItem = (JavaScriptObject)jsItems[key];
                            if (jsItem["value"].ToString() == value)
                            {
                                strb.AppendFormat("<input id=\"{0}\" checked=\"true\" name=\"{1}\" value=\"{2}\" type=\"radio\"><label for=\"{0}\">{3}</label>", jsItem["id"], name, jsItem["value"], jsItem["text"]);
                            }
                            else
                            {
                                strb.AppendFormat("<input id=\"{0}\" name=\"{1}\" value=\"{2}\" type=\"radio\"><label for=\"{0}\">{3}</label>", jsItem["id"], name, jsItem["value"], jsItem["text"]);
                            }

                        }
                        strb.Append("</span> ");
                    }
                    #endregion
                    break;
                case "select":
                    #region 添加 select
                    if (objTemp.ContainsKey("options"))
                    {
                        JavaScriptObject jsOptions = (JavaScriptObject)objTemp["options"];
                        strb.Append("<select name=\"" + name + "\" id=\"" + id + "\"  ");
                        if (objTemp.ContainsKey("attr"))
                        {
                            #region 添加属性
                            JavaScriptObject jsAttr = (JavaScriptObject)objTemp["attr"];
                            foreach (string key in jsAttr.Keys)
                            {
                                strb.AppendFormat(" {0}=\"{1}\" ", key, jsAttr[key]);
                            }
                            #endregion
                        }
                        strb.Append(">");
                        foreach (string key in jsOptions.Keys)
                        {
                            JavaScriptObject jsOption = (JavaScriptObject)jsOptions[key];
                            if (jsOption["value"].ToString() == value)
                            {
                                strb.AppendFormat(" <option value=\"{0}\" selected=\"selected\">{1}</option> ", jsOption["value"], jsOption["text"]);
                            }
                            else
                            {
                                strb.AppendFormat(" <option value=\"{0}\">{1}</option> ", jsOption["value"], jsOption["text"]);
                            }
                        }
                        strb.Append("</select> ");
                    }
                    #endregion
                    break;
                case "contentlink":
                    #region 添加 contentlink 的相关代码
                    strb.AppendLine(" <table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">");
                    strb.AppendLine("   <tbody>");
                    strb.AppendLine("<tr>");
                    strb.AppendLine("       <td>");
                    if (objTemp.ContainsKey("width"))//style=" width:100px;"
                    {
                        strb.AppendLine("             <select id=\"" + name + "_Links\" name=\"" + name + "_Links\" size=\"10\" style=\" width:" + objTemp["width"].ToString() + "px;\"  ></select>");
                    }
                    else
                    {
                        strb.AppendLine("             <select id=\"" + name + "_Links\" name=\"" + name + "_Links\" size=\"10\" style=\" width:90px;\"  ></select>");
                    }
                    strb.AppendLine("              <input name=\"" + name + "\" id=\"" + name + "\" type=\"hidden\" value=\"" + value + "\">");
                    strb.AppendLine("        </td>");
                    strb.AppendLine("        <td>");
                    strb.AppendLine("                &nbsp;<input   value=\"×\" onclick=\"del_links('" + name + "')\" type=\"button\"  class=\"button_style\"/><br/><br/>");
                    strb.AppendLine("                 &nbsp;<input   class=\"button_style\" value=\"∧\" onclick=\"moveUp_links('" + name + "')\"  type=\"button\"/><br/>");
                    strb.AppendLine("                &nbsp;<input   class=\"button_style\" value=\"∨\" onclick=\"moveDown_links('" + name + "')\"   type=\"button\"/><br/><br/>");
                    strb.AppendLine("                &nbsp;<input  class=\"button_style\" value=\"...\" onclick=\"editContentLink('" + name + "')\"  type=\"button\"/>");
                    strb.AppendLine("         </td>");
                    strb.AppendLine("          <td>");
                    strb.AppendLine("          </td>");
                    strb.AppendLine("       </tr>");
                    strb.AppendLine("    </tbody>");
                    strb.AppendLine(" </table>");
                    strb.AppendLine("<script type=\"text/javascript\">");
                    strb.AppendLine(" $(document).ready(function() {bindLinksData('" + name + "'); });");
                    strb.AppendLine("</script>");
                    #endregion
                    break;
                case "userdefinedlink":
                    #region 添加 contentlink 的相关代码
                    strb.AppendLine(" <table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">");
                    strb.AppendLine("   <tbody>");
                    strb.AppendLine("<tr>");
                    strb.AppendLine("       <td>");
                    if (objTemp.ContainsKey("width"))//style=" width:100px;"
                    {
                        strb.AppendLine("             <select id=\"" + name + "_Links\" name=\"" + name + "_Links\" size=\"10\" style=\" width:" + objTemp["width"].ToString() + "px;\"  ></select>");
                    }
                    else
                    {
                        strb.AppendLine("             <select id=\"" + name + "_Links\" name=\"" + name + "_Links\" size=\"10\" style=\" width:90px;\"  ></select>");
                    }
                    strb.AppendLine("              <input name=\"" + name + "\" id=\"" + name + "\" type=\"hidden\" value=\"" + value + "\">");
                    strb.AppendLine("        </td>");
                    strb.AppendLine("        <td>");
                    strb.AppendLine("                &nbsp;<input   value=\"×\" onclick=\"del_links('" + name + "')\" type=\"button\"  class=\"button_style\"/><br/><br/>");
                    //strb.AppendLine("                 &nbsp;<input   class=\"button_style\" value=\"∧\" onclick=\"moveUp_links('" + name + "')\"  type=\"button\"/><br/>");
                    //strb.AppendLine("                &nbsp;<input   class=\"button_style\" value=\"∨\" onclick=\"moveDown_links('" + name + "')\"   type=\"button\"/><br/><br/>");
                    strb.AppendLine("                &nbsp;<input  class=\"button_style\" value=\"...\" onclick=\"editUserDefinedLink('" + objTemp["valuetypeid"] + "','" + name + "')\"  type=\"button\"/>");
                    strb.AppendLine("         </td>");
                    strb.AppendLine("          <td>");
                    strb.AppendLine("          </td>");
                    strb.AppendLine("       </tr>");
                    strb.AppendLine("    </tbody>");
                    strb.AppendLine(" </table>");
                    strb.AppendLine("<script type=\"text/javascript\">");
                    strb.AppendLine(" $(document).ready(function() {bindUserDefinedData('" + name + "'); });");
                    strb.AppendLine("</script>");
                    #endregion
                    break;
                case "checkbox":
                    #region 添加 checkbox
                    if (objTemp.ContainsKey("options"))
                    {
                        JavaScriptObject jsOptions = (JavaScriptObject)objTemp["options"];
                        foreach (string key in jsOptions.Keys)
                        {
                            JavaScriptObject jsOption = (JavaScriptObject)jsOptions[key];
                            if (value == jsOption["value"].ToString() || value.StartsWith(jsOption["value"].ToString() + ",") || value.EndsWith("," + jsOption["value"].ToString()) || value.Contains("," + jsOption["value"].ToString() + ","))
                            {
                                strb.AppendFormat(" <input type=\"checkbox\" name=\"{0}\" value=\"{1}\"  checked=checked  /> {2}   ", name, jsOption["value"], jsOption["text"]);
                            }
                            else
                            {
                                strb.AppendFormat(" <input type=\"checkbox\" name=\"{0}\" value=\"{1}\"    /> {2}   ", name, jsOption["value"], jsOption["text"]);
                            }
                        }
                    }
                    #endregion
                    break;
                case "news_editor":
                case "ckeditor":
                case "xheditor":
                case "html_editor":
                    #region xheditor
                    //strb.AppendLine(GetAdvanceControlForXHEditor(id.ToString()));//为XHEditor编辑器添加基本设置
                    strb.AppendFormat(" <input type=\"hidden\" name=\"{0}_hidden\" id=\"{0}_hidden\" value=\"{1}\" />", id, Microsoft.JScript.GlobalObject.escape(value));
                    strb.AppendFormat(" <textarea   rows=\"30\" cols=\"500\"    name=\"{0}\" id=\"{1}\"  style=\" width:{2}; height:{3}\"></textarea>\n", name, id, strEditorWidth, strEditorHeight);
                    strb.AppendLine("   <script type=\"text/javascript\">");
                    strb.AppendFormat("   $(function () {{ InitEditor('{0}', '{3}', '{1}', '{2}',true); }});  InsertBaseControl('{0}','{3}', '{1}', '{2}'); ", id, strEditorWidth, strEditorHeight, objTemp["type"].ToString());
                    strb.AppendLine("   </script>");
                    #endregion
                    break;
                default:
                    break;
            }
            return strb.ToString();
        }

        #endregion

        #region 添加form元素 hidden
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Addhidden(string name, string id, string value)
        {
            StringBuilder strb = new StringBuilder();
            strb.Append(string.Format(@"<input "));
            strb.Append(string.Format("   type=\"hidden\"  id=\"{0}\"  name=\"{1}\"    value=\"{2}\"  ", id, name, value));
            strb.Append(string.Format(@"/>    "));
            return strb.ToString();
        }

        #endregion

        #region 添加form元素 textarea
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="rows"></param>
        /// <param name="cols"></param>
        /// <param name="value"></param>
        /// <param name="objTemp"></param>
        /// <returns></returns>
        public static string Addtextarea(string name, string id, int rows, int cols, string value, JavaScriptObject objTemp)
        {
            StringBuilder strb = new StringBuilder();
            strb.Append(string.Format(@"<textarea "));
            strb.Append(string.Format("   id=\"{0}\"  name=\"{1}\"  {2}  rows=\"{3}\" cols=\"{4}\" ", id, name, m_txtClassStr, rows, cols));
            #region 添加其他属性
            if (objTemp != null && objTemp.Count > 0)
            {
                //readOnly
                if (objTemp.ContainsKey(HtmlAttributeKey.TextAreaReadOnly) && objTemp[HtmlAttributeKey.TextAreaReadOnly].ToString().ToLower() == "true")
                {
                    strb.Append("   readonly=\"readonly\" disabled=\"disabled\" ");
                }
                //结合js验证文本框
                if (objTemp.ContainsKey(HtmlAttributeKey.TextAreaRequired) && objTemp[HtmlAttributeKey.TextAreaRequired].ToString().ToLower() == "true")
                {
                    strb.Append(string.Format("   required=\"1\"   "));
                    //prompt
                    if (objTemp.ContainsKey(HtmlAttributeKey.TextAreaPrompt))
                    {
                        strb.Append(string.Format("   prompt=\"{0}\" ", objTemp[HtmlAttributeKey.TextAreaPrompt]));
                    }
                    //valuelength
                    if (objTemp.ContainsKey(HtmlAttributeKey.TextAreaValuelength))
                    {
                        strb.Append(string.Format("   valuelength=\"{0}\" ", objTemp[HtmlAttributeKey.TextAreaValuelength]));
                    }
                    //valuetype
                    if (objTemp.ContainsKey(HtmlAttributeKey.TextAreaValuetype))
                    {
                        strb.Append(string.Format("   valuetype=\"{0}\" ", objTemp[HtmlAttributeKey.TextAreaValuetype]));
                    }
                }
            }
            strb.Append(string.Format(@">{0}</textarea>", value));
            #endregion
            return strb.ToString();
        }

        #endregion

        #region  添加form元素 radio

        /// <summary>
        ///  添加form元素 radio
        /// </summary>
        /// <param name="name">radio的name</param>
        /// <param name="JsList"> 请根据TRPOP.Common.HtmlAttributeKey 进行枚举Key  RadioValue RadioShowName 必须指定</param>
        /// <returns></returns>
        public static string Addradion(string name, List<JavaScriptObject> JsList)
        {
            StringBuilder strb = new StringBuilder();
            if (JsList.Count <= 0 || name.IsEmpty())
                return "";
            foreach (JavaScriptObject objTemp in JsList)
            {
                if (objTemp.ContainsKey(HtmlAttributeKey.RadioShowName) && objTemp.ContainsKey(HtmlAttributeKey.RadioValue))
                {
                    if (objTemp.ContainsKey(HtmlAttributeKey.Radiochecked) && objTemp[HtmlAttributeKey.Radiochecked].ToString().ToLower() == "true")
                    {
                        strb.Append(string.Format("<input type=\"radio\"  name=\"{0}\" value=\"{1}\" checked=\"checked\" />{2}",
                                                  name,
                                                  objTemp[HtmlAttributeKey.RadioValue].ToString(),
                                                  objTemp[HtmlAttributeKey.RadioShowName].ToString()
                                                  )
                                    );
                    }
                    else
                    {
                        strb.Append(string.Format("<input type=\"radio\"  name=\"{0}\" value=\"{1}\"  />{2}",
                                                 name,
                                                 objTemp[HtmlAttributeKey.RadioValue].ToString(),
                                                 objTemp[HtmlAttributeKey.RadioShowName].ToString()
                                                 )
                                   );
                    }
                }
                strb.Append("");
            }
            return strb.ToString();
        }

        #endregion

        #region 添加编辑器的高级选项
        /// <summary>
        /// 添加编辑器的高级选项 【适合ckeditor】
        /// </summary>
        /// <param name="strControlID"></param>
        /// <returns></returns>
        public static string GetAdvanceControlForCKEditor(string strControlID)
        {
            string strescape = "&nbsp;&nbsp;";
            StringBuilder strbuild = new StringBuilder();
            strbuild.Append("基本操作和设置：");
            strbuild.AppendFormat("<input class=\"button_style\" title=\"粘贴进来(目前只支持IE内核)\" name=\"PastToControl\" onclick=\"CKEDITOR.tools.callFunction(24, this);return false;\" value=\"粘贴进来\"  type=\"button\" />{1}", strControlID, strescape);
            strbuild.AppendFormat("<input class=\"button_style\" title=\"将HTML标签全部转换成P标签\" name=\"AdvanceFormatTextControl\" onclick=\"AdvanceFormatText('{0}',CKEDITOR.instances.{0}.getData());return false;\" value=\"格式化\"  type=\"button\" />{1}", strControlID, strescape);
            strbuild.AppendFormat("<input class=\"button_style\" title=\"简繁转换\" name=\"S2TControl\" id=\"S2TControl\" onclick=\"return ChangeSimplifie2TraditionalFont($('#{0}').val(),'S2TControl','{0}');\" value=\"简->繁\"  type=\"button\" />{1}", strControlID, strescape);
            strbuild.AppendFormat("<input name=\"chkPicLocating\" id=\"chkPicLocating\" type=\"checkbox\"  checked=\"checked\"  value=\"true\"  /> 图片是否本地化{0}", strescape);
            strbuild.AppendFormat("<input name=\"chkPicWater\"    id=\"chkPicWater\"    type=\"checkbox\"  value=\"true\"   />图片是否加水印{0}", strescape);
            strbuild.AppendFormat(@"水印位置:
                                    <select name='chkPicWaterSelect' id='chkPicWaterSelect'>
                                        <option value='1'>左上角</option>
	                                    <option value='2'>右上角</option>
	                                    <option value='3'>左下角</option>
                                        <option value='4' selected>右下角</option>
                                        <option value='5'>居中偏下</option>
                                    </select>{0}", strescape);
            return strbuild.ToString();
        }

        #endregion



    }
}
